Class {
	#name : 'WindowAnnouncementTest',
	#superclass : 'TestCase',
	#instVars : [
		'window'
	],
	#category : 'Morphic-Tests-WindowNotification',
	#package : 'Morphic-Tests',
	#tag : 'WindowNotification'
}

{ #category : 'running' }
WindowAnnouncementTest >> tearDown [
	window ifNotNil: [ window delete ].
	super tearDown
]

{ #category : 'tests - window change' }
WindowAnnouncementTest >> testCollapsing [
	| t |
	window := SystemWindow labelled: 'foo'.
	t := 0.
	window openInWorld.
	window announcer when: WindowCollapsed do: [ :ann | t := t + 1 ] for: self.
	self assert: t equals: 0.
	window collapse.
	self assert: t equals: 1
]

{ #category : 'tests - window change' }
WindowAnnouncementTest >> testMoving [
	| t oldBounds event |
	window := SystemWindow labelled: 'foo'.
	t := 0.
	event := nil.

	window openInWorld.
	oldBounds := window bounds.
	window announcer
		when: WindowMoved
		do: [ :ann |
			t := t + 1.
			event := ann ]
		for: self.

	self assert: t equals: 0.
	self assert: event isNil.

	"We move the window"
	window position: 50 @ 50.
	self assert: t equals: 1.
	self assert: event oldPosition equals: oldBounds origin.
	self assert: event newPosition equals: 50 @ 50.

	"We call position: without moving it actually, nothing should happen"
	window position: 50 @ 50.
	self assert: t equals: 1.

	"If we simply resize the window, nothing should happen as well"
	window extent: 50 @ 60.
	self assert: t equals: 1
]

{ #category : 'tests - window change' }
WindowAnnouncementTest >> testResizing [
	| t oldBounds newBounds |
	window := SystemWindow labelled: 'foo'.
	window setProperty: #minimumExtent toValue: 1 @ 1.
	t := 0.
	window openInWorld.
	oldBounds := window bounds.
	window announcer when: WindowResizing do: [ :ann | t := t + 1 ] for: self.
	self assert: t equals: 0.
	window extent: 50 @ 60.
	newBounds := window bounds.
	self assert: t equals: 1
]

{ #category : 'tests - window creation and deletion' }
WindowAnnouncementTest >> testResizingClosing [
	| coll |
	window := SystemWindow labelled: 'foo'.
	coll := OrderedCollection new.
	window openInWorld.
	window announcer when: WindowAnnouncement do: [ :ann | coll add: ann ] for: self.
	self assertEmpty: coll.
	window minimizeOrRestore.

	"Resizing, moving, deActivation, collapsing "
	self assert: coll size equals: 4.
	self assert: coll first isResized.
	self assert: coll second isMoved.
	self assert: coll third isDeActivated.
	self assert: coll fourth isCollapsed.

	window delete.

	self assert: coll size equals: 5.
	self assert: coll fifth isClosed.
	window := nil
]

{ #category : 'tests - window change' }
WindowAnnouncementTest >> testScrolling [
	"This test tests the scrolling values of a scrollpane. We create a reeeeeally big (enormous) morph, to be sure its size is bigger than the screen and therefore its enclosing window has scrollbars.
	When screen sizes become bigger, you should increase the value, or fix morphic, what happens first :).
	"

	| myMorph pane t |
	window := SystemWindow labelled: 'foo'.
	window extent: 300 @ 200.
	myMorph := Morph new.
	myMorph extent: 10000000000 @ 1000000000.
	pane := ScrollPane new.
	pane scroller addMorph: myMorph.
	window addMorph: pane fullFrame: LayoutFrame identity.
	t := 0 @ 0.
	window openInWorld.
	window announcer when: WindowScrolling do: [ :ann | t := t + ann step ] for: self.
	pane hScrollBarValue: 10.
	pane vScrollBarValue: 5.

	window delete.

	self assert: t equals: 10 @ 5.
	window := nil
]

{ #category : 'tests - window creation and deletion' }
WindowAnnouncementTest >> testWindowCreation [

	| t oldBounds newBounds |
	t := 0.
	[
	self currentWorld announcer when: WindowResizing do: [ :ann | t := t + 1 ] for: self.
	window := SystemWindow labelled: 'foo'.
	window setProperty: #minimumExtent toValue: 1 @ 1.
	window openInWorld.
	oldBounds := window bounds.
	window announcer when: WindowResizing do: [ :ann | t := t + 1 ] for: self.
	self assert: t equals: 0.
	window extent: 50 @ 60.
	newBounds := window bounds.
	self assert: t equals: 1 ] ensure: [ self currentWorld announcer unsubscribe: self ]
]

{ #category : 'tests - window creation and deletion' }
WindowAnnouncementTest >> testWindowCreationAndDeletion [

	| t newWindowCreated oldValue |
	oldValue := SystemWindow useHideForClose.
	[
	SystemWindow useHideForClose: false. "This is needed because else the windows close announcement will not be fired now."
	t := 0.
	self currentWorld announcer
		when: WindowOpened
		do: [ :ann |
			t := t + 1.
			newWindowCreated := ann window ]
		for: self.
	self currentWorld announcer
		when: WindowClosed
		do: [ :ann |
			t := t + 10.
			newWindowCreated := ann window ]
		for: self.
	window := SystemWindow labelled: 'foo'.
	window openInWorld.

	self assert: t equals: 1.
	self assert: window identicalTo: newWindowCreated.
	window delete.

	self assert: t equals: 11.
	self assert: window identicalTo: newWindowCreated ] ensure: [
		SystemWindow useHideForClose: oldValue.
		self currentWorld announcer unsubscribe: self ]
]

{ #category : 'tests - window creation and deletion' }
WindowAnnouncementTest >> testWindowLabelling [
	"Test change of label for a window."

	| labels win |
	labels := #(  ).
	[
	self currentWorld announcer
		when: WindowLabelled
		do: [ :ann |
			win := ann window.
			labels := labels copyWith: ann label ]
		for: self.
	window := SystemWindow labelled: 'foo'.
	window openInWorld.
	self assert: win equals: window.
	self assert: labels equals: #( 'foo' ).
	window setLabel: 'bar'.
	self assert: win equals: window.
	self assert: labels equals: #( 'foo' 'bar' ) ] ensure: [ self currentWorld announcer unsubscribe: self ]
]
